{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6a0d5504-9dbb-4499-a5c8-62465c111f32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np  # 导入numpy\n",
    "n1 = np.array([1,2,3])\n",
    "print(n1)"
   ]
  },
  {
   "cell_type": "code",
   "id": "f5c7f237-c550-4e96-b3ff-18317c48e80a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-10T14:39:51.743446Z",
     "start_time": "2025-07-10T14:39:51.512585Z"
    }
   },
   "source": [
    "# 创建一个带小数点的数组\n",
    "n2 = np.array([0,1,2,0.3])\n",
    "n2"
   ],
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[1], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# 创建一个带小数点的数组\u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m n2 \u001B[38;5;241m=\u001B[39m \u001B[43mnp\u001B[49m\u001B[38;5;241m.\u001B[39marray([\u001B[38;5;241m0\u001B[39m,\u001B[38;5;241m1\u001B[39m,\u001B[38;5;241m2\u001B[39m,\u001B[38;5;241m0.3\u001B[39m])\n\u001B[0;32m      3\u001B[0m n2\n",
      "\u001B[1;31mNameError\u001B[0m: name 'np' is not defined"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2859f5d0-5c07-4cd2-98c9-6391f471da5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个二维数组\n",
    "n3 = np.array([[1, 2], [3, 4]])\n",
    "n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "531e06ec-7a12-4530-b338-ebcf726eb4fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n",
      "float64\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.float64'>\n"
     ]
    }
   ],
   "source": [
    "# 创建数组时指定数据类型\n",
    "n4 = np.array([1, 2, 3], dtype=float)\n",
    "print(n4)\n",
    "print(n4.dtype)\n",
    "print(type(n4))\n",
    "print(type(n4[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a32236e9-6e0b-4cf6-bed7-57bd4a42b9f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[100   2 200]\n"
     ]
    }
   ],
   "source": [
    "# 数组复制\n",
    "n5 = np.array([1, 2, 3])\n",
    "n6 = np.array(n5, copy=True)\n",
    "n6[0]=100\n",
    "n6[2]=200\n",
    "print(n5)\n",
    "print(n6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9df91f10-ed22-448e-82ca-91235e10a499",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2 3]]]\n"
     ]
    }
   ],
   "source": [
    "# 指定维数\n",
    "lst=[1,2,3]\n",
    "n7=np.array(lst, ndmin=3)  # 最小维数， 创建三维数组\n",
    "print(n7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "441069b2-e260-4396-8fbb-1decd8ecb83a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6.23042070e-307, 1.29060531e-306, 9.79107872e-307],\n",
       "       [1.60218491e-306, 2.22522597e-306, 1.33511969e-306],\n",
       "       [1.11261027e-306, 1.29061142e-306, 6.23057349e-307],\n",
       "       [9.34609111e-307, 1.11261095e-306, 1.42410974e-306]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不同方式创建数组\n",
    "n8=np.empty([4, 3])  #  [4, 3]指的是四行三列，其中的数值是随机的, 可指定类型，只需要在后面加上dtype=int\n",
    "n8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "90a0354c-ebef-41fa-8ede-9e7eb5d33ec3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0.])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建指定的维度，以0填充\n",
    "n9=np.zeros(3)\n",
    "n9 # 输出结果默认是浮点类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7f456576-cf88-479a-9b78-2f4fe8f2d71d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1.])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建指定维度，以1填充\n",
    "n10=np.ones(3)\n",
    "n10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "199fd9af-cb6c-4de1-a249-e5c234cb37c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 8, 8])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建指定维度，以指定的数值填充\n",
    "n11 = np.full(3, 8) # 创建一维数组，以8填充三个\n",
    "n11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "02083cd3-0fee-4c7b-bb3c-e90faa404776",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 8, 8, 8],\n",
       "       [8, 8, 8, 8],\n",
       "       [8, 8, 8, 8]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建指定维度，以指定的数值填充\n",
    "n12 = np.full((3, 4), 8) # 创建二维数组，三行四列，以8填充三个\n",
    "n12"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
